﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Runtime.InteropServices;

namespace LaneLpnr.Lpnr
{
    public class LPNRProgram
    {

      Common.WriteLog trace = new Common.WriteLog();

        //初始化摄像机
        [DllImport("HVDLL_001.dll", EntryPoint = "InitHv", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern int InitHv(string ip);
        //关闭
        [DllImport("HVDLL_001.dll", EntryPoint = "CloseHv", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern int CloseHv(IntPtr handle);

        [DllImport("HVDLL_001.dll", EntryPoint = "SetHvSource", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern int SetHvSource(IntPtr handle, int width, int height);


        //________________lpnr动态库______________________

        //创建LED设备对象  返回设备对象句柄
        [DllImport("LPNR.dll", EntryPoint = "LPNR_Init", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern IntPtr LPNR_Init(string strIP);

        //消息接收窗口(Windows)
        [DllImport("LPNR.dll", EntryPoint = "LPNR_SetWinMsg", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern IntPtr LPNR_SetWinMsg(IntPtr h, IntPtr hwnd, int msgno);// h mable is LPNR_Init back intptr ， hwnd mable is window  intptr

        //是否连线，返回布林值
        [DllImport("LPNR.dll", EntryPoint = "LPNR_IsOnline", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_IsOnline(IntPtr h);

        //识别机是否进行识别处理中，返回布林值
        [DllImport("LPNR.dll", EntryPoint = "LPNR_IsIdle", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_IsIdle(IntPtr h);

        //获取当前实时图像帧
        //[DllImport("LPNR.dll", EntryPoint = "LPNR_GetLiveFrame", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        //public static extern int LPNR_GetLiveFrame(IntPtr h, byte[] buf);


        //获取当前实时图像帧
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetLiveFrame", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetLiveFrame(IntPtr h, byte[] buf);

        //获取当前实时图像帧大小
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetLiveFrameSize", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetLiveFrameSize(IntPtr h);

        ////禁止与是能实时视频   
        //[DllImport("LPNR.dll", EntryPoint = "LPNR_EnableLiveFrame", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        //public static extern bool LPNR_EnableLiveFrame(IntPtr h, bool bit);

        //1.3.5.18 或以上的版本
        //禁止与是能实时视频   
        [DllImport("LPNR.dll", EntryPoint = "LPNR_EnableLiveFrame", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_EnableLiveFrame(IntPtr h, int nSizeCode);

        //软触发抓拍识别 - 抓拍 + 识别
        [DllImport("LPNR.dll", EntryPoint = "LPNR_SoftTrigger", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_SoftTrigger(IntPtr h);

        //获取抓拍整图
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetCapturedImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetCapturedImage(IntPtr h, byte[] buf);

        //获取车牌彩色小图
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetPlateColorImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetPlateColorImage(IntPtr h, byte[] buf);

        //获取车牌二值化小图
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetPlateBinaryImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetPlateBinaryImage(IntPtr h, byte[] buf);

        //返回抓拍大图大小
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetCapturedImageSize", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetCapturedImageSize(IntPtr h);

        //获取车头图大小（如有使能发送车头图）
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetHeadImageSize", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetHeadImageSize(IntPtr h);

        //获取1/4图图片大小（如有使能发送抓拍小图）
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetQuadImageSize", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetQuadImageSize(IntPtr h);

        //获取车牌彩色小图大小
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetPlateColorImageSize", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetPlateColorImageSize(IntPtr h);

        //返回车牌二值化小图大小
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetPlateBinaryImageSize", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetPlateBinaryImageSize(IntPtr h);

        //获取车头图
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetHeadImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetHeadImage(IntPtr h, byte[] buf);

        //获取1/4图
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetQuadImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int LPNR_GetQuadImage(IntPtr h, byte[] buf);

        //获取车牌号码
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetPlateNumber", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        //public static extern bool LPNR_GetPlateNumber(IntPtr h, ref byte dest);
        public static extern bool LPNR_GetPlateNumber(IntPtr h, byte[] dest);

        //获取识别机IP字串
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetMachineIP", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_GetMachineIP(IntPtr h, byte[] buf);

        //获取识别机标签（名字）
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetCameraLabel", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern string LPNR_GetCameraLabel(IntPtr h);

        //获取车牌/车辆额外信息
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetPlateAttribute", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_GetPlateAttribute(IntPtr h, byte[] buf);

        //上锁（获取实时帧或是任何识别结果数据之前先加锁，获取完后解锁，避免获取过程中被工作线程更改。
        [DllImport("LPNR.dll", EntryPoint = "LPNR_Lock", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_Lock(IntPtr h);

        //解锁数据
        [DllImport("LPNR.dll", EntryPoint = "LPNR_Unlock", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_Unlock(IntPtr h);

        //释放当前识别结果动态分配的数据
        [DllImport("LPNR.dll", EntryPoint = "LPNR_ReleaseData", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_ReleaseData(IntPtr h);

        //同步识别机时间和计算机时间
        [DllImport("LPNR.dll", EntryPoint = "LPNR_SyncTime", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_SyncTime(IntPtr h);

        //获取识别机完成当前识别处理消耗的时间 （总过程时间和总处理时间）
        [DllImport("LPNR.dll", EntryPoint = "LPNR_GetTiming", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_GetTiming(IntPtr h, ref int elaped, ref int processed);

        //串口初始化
        [DllImport("LPNR.dll", EntryPoint = "LPNR_COM_init", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_COM_init(IntPtr h, int Speed);

        //串口发送
        [DllImport("LPNR.dll", EntryPoint = "LPNR_COM_send", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_COM_send(IntPtr h, byte[] buf, int len);

        //串口使能
        [DllImport("LPNR.dll", EntryPoint = "LPNR_COM_aync", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_COM_aync(IntPtr h, bool bEnable);

        //结束
        [DllImport("LPNR.dll", EntryPoint = "LPNR_Terminate", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_Terminate(IntPtr h);

        [DllImport("LPNR.dll", EntryPoint = "LPNR_SetCallBack", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool LPNR_SetCallBack(IntPtr h, LPNRCallbackDelegate callback);
        public delegate void LPNRCallbackDelegate(IntPtr h, int a); //声明委托  

        //________________万集动态库______________________

        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_Init", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool WJLS_DVR_Init();

        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_Login", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern Int32 WJLS_DVR_Login(string sIP, int wPort, string sUserName, string sPassword);

        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_Logout", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool WJLS_DVR_Logout(Int32 h);

        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_ForceCapture", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool WJLS_DVR_ForceCapture(Int32 h);
        //大图
        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_GetBigImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int WJLS_DVR_GetBigImage(byte[] ImgBuf, int bufLen, ref int ImgSize);
        //二值图
        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_GetBinaryImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int WJLS_DVR_GetBinaryImage(byte[] ImgBuf, int bufLen, ref int ImgSize);
        //彩图
        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_GetSmallImage", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int WJLS_DVR_GetSmallImage(byte[] ImgBuf, int bufLen, ref int ImgSize);
        //获取车牌号码
        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_GetPlateNo", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern int WJLS_DVR_GetPlateNo(byte[] pPlateNo, int bufLen, ref int plateNoLen);

        //获取实时视频
        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_RealPlay", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern Int32 WJLS_DVR_RealPlay(Int32 lHandle, IntPtr FormHwnd);


        [DllImport("WJNVR.dll", EntryPoint = "WJLS_DVR_SetDVRMessCallBack", SetLastError = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern bool WJLS_DVR_SetDVRMessCallBack(CallbackDelegate callback, Int32 h);

        public delegate void CallbackDelegate(int a, IntPtr h); //声明委托 

    }
}
